﻿using System;

namespace SkypeBot.Commands
{
    using SkypeBot.Core;

    class AnecdoteCommand : IMessageHandler
    {
        [Message(@"^!a$|а$|анек$|анекдот$|шутка$|жарт$")]
        public Responce GetAnecdote(Message message)
        {
            if (Core.Instance.ArticleManager == null)
            {
                Core.Instance.ArticleManager = new Article();
            }
            Article ArticleManager = Core.Instance.ArticleManager;
            string strResult;
            if(ArticleManager.Articles==null)
            {
                ArticleManager.URIs = new [] { "http://tvoyanekdot.ru/rss/", "http://www.hooligani.ru/rss/novi-anekdot.xml", "http://www.anekdot.ru/rss/export_j_non_burning.xml", "http://www.anekdot.ru/rss/export_j.xml" };
                ArticleManager.Articles = RSSParser.GetRecords(ArticleManager.URIs[ArticleManager.CurrentUri]);
            }
            strResult = string.Format("{0}\r\n", ArticleManager.Articles[ArticleManager.CurrentArticle].Description);
            if ((ArticleManager.CurrentArticle + 1) < ArticleManager.Articles.Count)
            {
                ArticleManager.CurrentArticle++;
            }
            else
            {
                if ((ArticleManager.CurrentUri + 1) < ArticleManager.URIs.Length)
                {
                    ArticleManager.CurrentUri++;
                    ArticleManager.CurrentArticle = 0;
                    ArticleManager.Articles = RSSParser.GetRecords(ArticleManager.URIs[ArticleManager.CurrentUri]);
                }
                else 
                {
                    ArticleManager.CurrentUri = 0;
                    ArticleManager.CurrentArticle = 0;
                    ArticleManager.Articles = RSSParser.GetRecords(ArticleManager.URIs[ArticleManager.CurrentUri]);
                }
            }
            strResult = strResult.Replace("<br />", Environment.NewLine);
            strResult = strResult.Replace("<br>", Environment.NewLine);
            return new Responce(strResult); 
        }
    }
}
